package com.xiaoxx.rabbitmq.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * rabbitme 自动配置类
 * @author quhui
 * @date 2024/9/10 11:16
 */
@Slf4j
@Configuration
public class RabbitMqConfig {

    @Resource
    private RabbitProperties properties;


    // 存在此名字的bean 自带的连接工厂会不加载（也就是说yml中rabbitmq下一级不生效），如果想自定义来区分开 需要改变bean 的名称
    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory cachingConnectionFactory=new CachingConnectionFactory();
        cachingConnectionFactory.setUsername(properties.getUsername());
        cachingConnectionFactory.setPassword(properties.getPassword());
        cachingConnectionFactory.setHost(properties.getHost());
        cachingConnectionFactory.setPort(properties.getPort());
        cachingConnectionFactory.setVirtualHost(properties.getVirtualHost());

        //设置 开启发布确认
        cachingConnectionFactory.setPublisherReturns(true);
        cachingConnectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);

        cachingConnectionFactory.setCacheMode(properties.getCache().getConnection().getMode());
        return  cachingConnectionFactory;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
        containerFactory.setConnectionFactory(connectionFactory());

        // 并发消费者数量
        containerFactory.setConcurrentConsumers(properties.getListener().getSimple().getConcurrency());
        containerFactory.setMaxConcurrentConsumers(properties.getListener().getSimple().getMaxConcurrency());
        containerFactory.setAcknowledgeMode(properties.getListener().getSimple().getAcknowledgeMode());
        containerFactory.setPrefetchCount(properties.getListener().getSimple().getPrefetch());
        //消息序列化方式
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.registerModule(new JavaTimeModule());
        containerFactory.setMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
        containerFactory.setDefaultRequeueRejected(properties.getListener().getSimple().getDefaultRequeueRejected());
        return containerFactory;
    }



    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.initialize();
        // for (RabbitQueueExchangeConstant.BindingList bindingEnum : RabbitQueueExchangeConstant.BindingList.values()) {
        //     Queue queue = bindingEnum.getQueueEnum().getQueue();
        //     Exchange exchange = bindingEnum.getExchangeEnum().getExchange();
        //     Binding binding = BindingBuilder.bind(queue).to(exchange).with(bindingEnum.getRoutingKey()).and(bindingEnum.getArguments());
        //     rabbitAdmin.declareQueue(queue);
        //     rabbitAdmin.declareExchange(exchange);
        //     rabbitAdmin.declareBinding(binding);
        // }
        return rabbitAdmin;
    }


}
